package org.luckyjourney.service.agent.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.luckyjourney.entity.File;
import org.luckyjourney.entity.agent.AgentChatTable;
import org.luckyjourney.entity.agent.AgentTable;
import org.luckyjourney.entity.vo.AgentPageVo;
import org.luckyjourney.holder.UserHolder;
import org.luckyjourney.mapper.agent.AgentTableMapper;
import org.luckyjourney.service.FileService;
import org.luckyjourney.service.agent.AgentChatTableService;
import org.luckyjourney.service.agent.AgentTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 86155
* @description 针对表【agent_table(2智能体表)】的数据库操作Service实现
* @createDate 2024-03-21 15:10:29
*/
@Service
public class AgentTableServiceImpl extends ServiceImpl<AgentTableMapper, AgentTable>
    implements AgentTableService {

    @Autowired
    private AgentTableMapper agentTableMapper;

    @Autowired
    private FileService fileService;

    @Autowired
    private AgentChatTableService agentChatTableService;

    @Override
    public IPage<AgentTable> pageAgentTable(Page<AgentTable> page) {
        IPage<AgentTable> agentTableIPage = agentTableMapper.selectPage(page, null);
        return agentTableIPage;
    }

    @Override
    public IPage<AgentTable> getByName(String agentName, AgentPageVo agentPageVo) {
        Page<AgentTable> page = new Page<>();
        /*IPage<AgentTable> agentTableIPage=agentTableMapper.selectPageByName(page, agentName);*/
        LambdaQueryWrapper<AgentTable> wrapper = new LambdaQueryWrapper<>();
        // 添加查询条件，例如：
         wrapper.like(AgentTable::getAgentName, agentName);
        IPage<AgentTable> agentTableIPage = agentTableMapper.selectPage(page, wrapper);
        List<AgentTable> agentList = agentTableIPage.getRecords();
        //将头像照片赋值进去
        agentList.forEach(comment -> {
            if(!StringUtils.isEmpty(comment.getAgentProfile())){
                Long avatar = Long.valueOf(comment.getAgentProfile());
                File fileTrustUrl = fileService.getFileTrustUrl(avatar);
                if (!ObjectUtils.isEmpty(fileTrustUrl)){
                    comment.setAgentProfile(fileTrustUrl.getFileKey());
                }
            }
        });
        //判断是否是已添加到对话列表
        List<AgentChatTable> tableList = agentChatTableService.list(new LambdaQueryWrapper<AgentChatTable>().eq(AgentChatTable::getUserId, UserHolder.get()));
        tableList.forEach(table -> {
            agentList.forEach(agent -> {
                if (agent.getAgentId().equals(table.getAgentId())){
                    agent.setInChat(true);
                    agent.setAgentChatId(table.getAgentChatId());
                }
            });
        });
        //用户将智能体添加到对话的数量
        agentList.forEach(agent -> {
            agent.setChatsCount(agentChatTableService.count(new LambdaQueryWrapper<AgentChatTable>().eq(AgentChatTable::getAgentId, agent.getAgentId())));
        });
        return agentTableIPage;
    }

    @Override
    public AgentTable getOneAgnet(Long couselor_id) {
        AgentTable agentTable=agentTableMapper.selectOneAgent(couselor_id);
        return agentTable;
    }


}




